*********************************************************************************
/*    Create scatter plot for revenue, cost, and revenue/cost by income		*/
*********************************************************************************
clear all

* File paths
local datapath "${mvpf_audits}/data/cleaned" 
local rawdatapath "${mvpf_audits}/data/outside"
local graphpath "${graphpath}" 
local dofilepath "${mvpf_audits}\code\prep" 

local multiplier_yr = 2003
local inc = "tpi"

di "${version}"

* Version of fig to produce 
global version  ${version} // paper or slides

* Other formatting
if "${version}" == "slides" {
	global title "title(" ", size(vhuge))"
	global img wmf
	graph set window fontface "Arial"
	global title "title(" ", size(vhuge))"
	global axis_size "" //xsize(6.5) ysize(4)" // makes figure wider
}

else if "${version}" == "paper" {
	global title "title("")"
	global img pdf
	graph set window fontface "LMRoman10-Regular"
	global title " "
	global axis_size " " // makes figure wider
}

/*------------------------------------------------------------------------------
Plot R, C, RC (alt 1 and alt 2) by TPI
------------------------------------------------------------------------------*/
use "${mvpf_audits}/data/figures_rep/ap4.dta", clear

preserve
	replace rev_tot_disc= 50000 if rev_tot_disc>50000
	local fig_name = "scatter_costs_rev_${inc}_alt_oh_allocation"
	* Set fig range (y-axis) for each audit type (ops and corr)
	if "`1'"== "ops" {
	    replace rev_tot_disc= 50000 if rev_tot_disc>50000
		local lb = 0
		local ub = 50000 // round(`max', 100)
		local int = 10000 // (`ub' - `lb')/5	
		global ylab "ylabel(0 "0" 10000 "10,000" 20000 "20,000" 30000 "30,000" 40000 "40,000" 50000 "95,500", nogrid format(%9.0fc))"
		}
			
	if "`1'"== "corr" {
	    replace rev_tot_disc= 6000 if rev_tot_disc>6000
		local lb = 0
		local ub = 6000 // round(`max', 100)
		local int = 1000 // (`ub' - `lb')/5	
		global ylab "ylabel(0 "0" 1000 "1,000"  2000 "2,000" 3000 "3,000" 4000 "4,000" 5000 "5,000" 6000 " 21,000", nogrid format(%9.0fc))"
		} 

	tw  (scatter rev_tot_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22), ///
		mcolor(sea) msymbol(C) msize(`symbsize')) ///
		(scatter costs_tot_future ${inc}_pctl if inrange(${inc}_pctl, 1, 22) , ///
		mcolor(cranberry*1.5) msymbol(T) msize(`symbsize')) ///
		(scatter costs_tot_alt1 ${inc}_pctl if inrange(${inc}_pctl, 1, 22) , ///
		mcolor(cranberry*1) msymbol(T) msize(`symbsize')) ///
		(scatter costs_tot_alt2 ${inc}_pctl if inrange(${inc}_pctl, 1, 22) , ///
		mcolor(cranberry*0.5) msymbol(T) msize(`symbsize')) ///
		, ///
		yscale(r()) ///
		ytitle("Dollars per audit ($)") xtitle("${xtitle}")   ///
		legend(order(1 2 3 4)  lab(1 "Audit revenue") lab(2 "Baseline allocation") lab(3 "Allocated by hours") ///
		lab(4 "Allocated by # of audits") ring(0) pos(11) col(1) symxsize(*3.5) region(lcolor(gs11))) ///
		///legend(off) ///
		${title} ${axis_size} ${ylab} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) ) 
	graph export "`graphpath'/`fig_name'.wmf", as(wmf) replace
restore

	* set y-axis label, title, and figure name
	local ylab = "Audit Revenue / Total Costs"
	lab val ${inc}_pctl ${inc}_pctl_label

	* Preferences 
	local symbsize small
	local textsize vsmall

	** Subgroup figure
	local fig_name = "scatter_rc_${inc}_alt_oh_allocation"
	
		* Set fig range (y-axis) for each audit type (ops and corr)
		local lb = 0
		local ub = 12 // round(`max', 100)
		local int = 2 // (`ub' - `lb')/5	
			

	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'
	
	** xtitle
	if "`inc'"=="agi" global xtitle = "AGI percentile group"
	if "`inc'"=="tpi" global xtitle = "TPI percentile group"
	
	** Add Arrows pointing to markers at 99+
	summ rc_future_disc if ${inc}_pctl==22 
	local rc_999: di %4.1f r(mean)
	
	summ rc_future_disc if ${inc}_pctl==21 
	local rc_99: di %4.1f r(mean)

	
	** Rev/Direct Labor Costs
	tw (scatter rc_future_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22) ,  ///
		 mcolor("${rc_col}*1.5") msymbol(S) msize(medsmall)) ///
		 (scatter rc_future_disc_alt1 ${inc}_pctl if inrange(${inc}_pctl, 1, 22) ,  ///
		 mcolor("${rc_col}") msymbol(S) msize(medsmall)) ///
		 (scatter rc_future_disc_alt2 ${inc}_pctl if inrange(${inc}_pctl, 1, 22) ,  ///
		 mcolor("${rc_col}*0.5") msymbol(S) msize(medsmall)) ///
		, ///
		yline(1, lw(thin) lp(dash) lc(gs11)) ///
		ylabel(`lb'(`int')`ub', nogrid format(%9.0fc)) yscale(r(`lb' `ub')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		legend(order(1 2 3)  lab(1 "Baseline allocation") lab(2 "Allocated by hours") ///
		lab(3 "Allocated by # of audits") ring(0) pos(11) col(1) symxsize(*3.5) region(lcolor(gs11))) ///
		${title} ${axis_size} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )
	graph export "`graphpath'/`fig_name'.pdf", replace